#include "LayerExit.h"

USING_NS_CC;

//////////////////////////////////////////////////////////////////////////
MenuItemImage * MenuItemImageScale::createScaleItem(const std::string& normalImage, const std::string& selectedImage, const ccMenuCallback& callback)
{
	MenuItemImageScale *ret = new MenuItemImageScale();
	if (ret && ret->initWithNormalImage(normalImage, selectedImage, "", callback))
	{
		ret->autorelease();
		return ret;
	}
	CC_SAFE_DELETE(ret);
	return nullptr;
}

void MenuItemImageScale::selected()
{
	if (_selectedImage == nullptr)
	{
		if (m_fOldScale <0.0000001 &&  m_fOldScale>-0.0000001)
		{
			m_fOldScale = getScale();
		}
		float fScale = (getContentSize().width*m_fOldScale+10)/getContentSize().width*m_fOldScale;
		setScale(fScale);
	}
	MenuItemImage::selected();	
}

void MenuItemImageScale::unselected()
{
	if (_selectedImage == nullptr)
	{
		if (m_fOldScale >=0.0000001 ||  m_fOldScale<=-0.0000001)
		{
			setScale(m_fOldScale);
		}
		else
		{
			setScale(1.0);
		}
	}
	MenuItemImage::unselected();	
}

//////////////////////////////////////////////////////////////////////////
bool LayerExit::init()
{
	bool ret = Layer::init();

	auto listenerKeyboard = cocos2d::EventListenerKeyboard::create();
	listenerKeyboard->onKeyReleased = CC_CALLBACK_2(LayerExit::onKeyReleased,this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerKeyboard,this);

	return ret;
}

void LayerExit::onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event)
{
	switch (keyCode)
	{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	case cocos2d::EventKeyboard::KeyCode::KEY_F1:
#else
	case cocos2d::EventKeyboard::KeyCode::KEY_BACK:
#endif
		{
			this->onLayerExit();
		}
		break;
	default:
		break;
	}
}
void LayerExit::onLayerExit()
{
    
}

void LayerExit::onUpdateView()
{

}